<?php

class Core_Util
{

    static public function getConfig($key)
    {
        $config_file = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini');
        $configs = $config_file->get("production")->toArray();
        return $configs[$key];
    }

    static public function getAllServices()
    {
        return array(
            "dunnage" => "Dunnage",
            "tally" => "Tally",
            "cargo" => "Cargo",
        );
    }

    static public function getAllMaterials()
    {
        return array(
            "dunnage" => "Dunnage Material",
            "tally" => "Tally Material",
        );
    }

    static public function getAllUnit()
    {
        return array(
            'M2' => 'M2',
            'K2' => 'K2',
            'L2' => 'L2',
        );
    }

    static public function getAdminActiveMenu($action, $params)
    {
        $service_type = NULL;
        if (isset($params['type'])) {
            $service_type = $params['type'];
        }
        $menus = array(
            'index' => array('active_menu' => 'home', 'active_submenu' => NULL),
            'service' => array('active_menu' => 'service', 'active_submenu' => $service_type),
            'ports' => array('active_menu' => 'ports', 'active_submenu' => 'list_port'),
            'editport' => array('active_menu' => 'ports', 'active_submenu' => 'list_port'),
            'newport' => array('active_menu' => 'ports', 'active_submenu' => 'list_port'),
            'buoys' => array('active_menu' => 'ports', 'active_submenu' => 'list_buoy'),
            'newbuoy' => array('active_menu' => 'ports', 'active_submenu' => 'list_buoy'),
            'editbuoy' => array('active_menu' => 'ports', 'active_submenu' => 'list_buoy'),
            'users' => array('active_menu' => 'users', 'active_submenu' => 'list_user'),
            'newuser' => array('active_menu' => 'users', 'active_submenu' => 'new_user'),
            'edituser' => array('active_menu' => 'users', 'active_submenu' => 'list_user'),
            'emailsetting' => array('active_menu' => 'emailsetting', 'active_submenu' => NULL),
            'about' => array('active_menu' => 'about', 'active_submenu' => null),
            'material' => array('active_menu' => 'material', 'active_submenu' => $service_type),
            "newdunnagematerial" => array('active_menu' => 'material', 'active_submenu' => 'dunnage'),
            "editdunnagematerial" => array('active_menu' => 'material', 'active_submenu' => 'dunnage'),
            "newtallyservice" => array('active_menu' => 'material', 'active_submenu' => 'tally'),
            "edittallyservice" => array('active_menu' => 'material', 'active_submenu' => 'tally'),
        );
        return $menus[$action];
    }

    static public function getCountryList()
    {
        $content = file_get_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'include' . DIRECTORY_SEPARATOR . 'countries');
        return explode("\r\n", $content);
    }

    static public function getRoleList()
    {
        return array(
            'Principal',
            'Input Data',
            'Admin'
        );
    }

    static public function hasAccess($transId)
    {
        if (!$transId) {
            return false;
        }
        $db = Zend_Registry::get('db');
        $session = Zend_Registry::get("session");
        $userId = isset($session->user)?$session->user->id:null;
        $role = Core_Util::getRole();

        switch (strtolower($role)) {
            case Core_Role::ROLE_PRINCIPAL:
                $query = "select * from transactions t
                          left join general_information g on g.id = t.general_information_id
                          where t.id = {$transId} and g.customer_id = {$userId}";
                $result = $db->fetchAll($query);
                if($result && is_array($result) && count($result) > 0){
                    return true;
                }else{
                    return false;
                }
                break;
            case Core_Role::ROLE_INPUT:
                $query = "select * from transactions t
                          where t.id = {$transId} and t.input_user = {$userId}";
                $result = $db->fetchAll($query);
                if($result && is_array($result) && count($result) > 0){
                    return true;
                }else{
                    return false;
                }

                break;
            case Core_Role::ROLE_ADMIN:
                return true;
                break;
            case Core_Role::ROLE_GUEST:
            default:
                return false;
        }
    }

    static public function isDebugMode()
    {
        return APPLICATION_ENV == 'development';
    }

    static public function rememberMe()
    {
        $max_time = self::getConfig("remember_me_time");
        Zend_Session::rememberMe($max_time);
    }

    static public function getRole()
    {
        $session = Zend_Registry::get("session");
        $user = $session->user;
        if ($user) {
            return $user["role"];
        } else {
            return null;
        }
    }

    public static function renderDate($time) {
        if ( !empty($time) ) {
            echo date(DATE_FORMAT, $time);
        }
    }
}

?>
